package com.june.junemplayer.view;

import android.animation.ObjectAnimator;
import android.annotation.SuppressLint;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.net.Uri;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.lifecycle.ViewModelStoreOwner;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.june.junemplayer.R;
import com.june.junemplayer.base.BaseActivity;
import com.june.junemplayer.controller.GlobalViewModelController;
import com.june.junemplayer.pojo.SongList;
import com.june.junemplayer.service.OverlayWindowService;
import com.june.junemplayer.service.OverlayWindowView;

import java.io.File;
import java.util.ArrayList;

public final class MainActivity extends BaseActivity implements View.OnClickListener {
    private Context mContext;
    private GlobalViewModelController mGlobalViewModelController;
    private OverlayWindowService mOverlayWindowService;


    private EditText mSearchInput;

    private TextView mUserName;
    private TextView mUserDescription;

    private TextView mConcernNumber;
    private TextView mFanNumber;

    private TextView mLoveSongsNumber;
    private TextView mLocalSongsNumber;

    private ImageView mUserSongListSwitch;
    private boolean mIsUserSongListOpen;

    private RecyclerView mUserSongList;
    private UserSongListAdapter mUserSongListAdapter;

    private RelativeLayout mNullSongListLayout;
    private boolean mIsNullSongList;

    @Override
    protected void initBeforeView(Bundle savedInstanceState) {
        Log.d("JuneProcedure", getTag() + "----initBeforeView");

        mContext = this;

        mGlobalViewModelController = new GlobalViewModelController((ViewModelStoreOwner) getApplication());
        mIsNullSongList = (mGlobalViewModelController.getCustomSongList() == null);
    }

    @Override
    protected void onPause() {
        super.onPause();
        Log.d("JuneProcedure", getTag() + "----onPause");
//        mGlobalViewModel.save();
    }

    @Override
    protected void bindView(Bundle savedInstanceState) {
        Log.d("JuneProcedure", getTag() + "----bindView");

        mSearchInput = findViewById(R.id.et_Search);

        mUserName = findViewById(R.id.tv_UserName);
        mUserDescription = findViewById(R.id.tv_UserDescription);

        mConcernNumber = findViewById(R.id.tv_ConcernNumber);
        mFanNumber = findViewById(R.id.tv_FanNumber);

        mLoveSongsNumber = findViewById(R.id.tv_LoveSongsNumber);
        mLocalSongsNumber = findViewById(R.id.tv_LocalSongsNumber);

        mUserSongListSwitch = findViewById(R.id.img_UserSongList);
        mIsUserSongListOpen = false;

        mUserSongList = findViewById(R.id.rcv_UserSongList);

        mNullSongListLayout = findViewById(R.id.rl_NullSongListSignal);
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        Log.d("JuneProcedure", getTag() + "----initData");

        Intent intent = new Intent(mActivity, OverlayWindowService.class);
        bindService(intent, connectionOverlayWindowServiceConnection, Context.BIND_AUTO_CREATE);

        mSearchInput.setOnClickListener(this);
        mSearchInput.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView textView, int actionId, KeyEvent keyEvent) {
                if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                    String SearchString = mSearchInput.getText().toString();

                    Log.d("June", "搜索---" + SearchString);

                    if (SearchString.equals("")) {
                        Log.d("June", (String) mSearchInput.getHint());
                    } else {
                        //进行搜索
//                        Intent intent = new Intent(aaa.this, MainActivity.class);
//                        intent.putExtra("title", str);
//                        startActivity(intent);
                    }
                }
                return false;
            }
        });

//        mUserName=
//        mUserDescription =

//        mConcernNumber=
//        mFanNumber=

        mLoveSongsNumber.setText(String.valueOf(mGlobalViewModelController.getLoveSongList().getNumber()));
        mLocalSongsNumber.setText(String.valueOf(mGlobalViewModelController.getSongs().size()));

        if (mIsNullSongList) {
            mUserSongListSwitch.setVisibility(View.INVISIBLE);
            mNullSongListLayout.setVisibility(View.VISIBLE);
        } else {
            mUserSongListSwitch.setVisibility(View.VISIBLE);
            mNullSongListLayout.setVisibility(View.GONE);
        }
        mUserSongListSwitch.setOnClickListener(this);
        mUserSongListSwitch.setImageResource(mIsUserSongListOpen ? R.drawable.ic_more_up : R.drawable.ic_more_down);

        mUserSongListAdapter = new UserSongListAdapter(mGlobalViewModelController.getCustomSongList());
        GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 1);
        mUserSongList.setLayoutManager(gridLayoutManager);
        mUserSongList.setAdapter(mUserSongListAdapter);


    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_main;
    }

    @Override
    protected String getTag() {
        return "MainActivity";
    }

    public void onClick(View view) {
        Log.d("JuneProcedure", getTag() + "----onClick");

        switch (view.getId()) {
            case R.id.et_Search:
                Log.d("June", "打开输入法");
                /**
                 * 悬浮窗和输入法同层级导致输入法无法打开
                 * 解决方式是点击隐藏悬浮窗打开输入法并监听
                 * 当输入法关闭时再打开悬浮窗
                 */
                break;
            case R.id.img_Settings:
                Log.d("June", "跳转至设置页面");
                break;
            case R.id.cv_PersonalInfo:
                Log.d("June", "跳转至个人页面");
                break;
            case R.id.img_UserHeadPortrait:
                Log.d("June", "跳转至设置头像");
                break;
            case R.id.rl_Concern:
                Log.d("June", "跳转至关注");
                break;
            case R.id.rl_Fan:
                Log.d("June", "跳转至粉丝");
                break;
            case R.id.rl_LoveSongs:
                Log.d("June", "跳转至喜欢");
                Intent intentToLoveSongList = new Intent(this, LoveSongListActivity.class);
                startActivity(intentToLoveSongList);
                break;
            case R.id.rl_LocalSongs:
                Log.d("June", "跳转至本地音乐");
                Intent intentToLocalSongList = new Intent(this, LocalSongListActivity.class);
                startActivity(intentToLocalSongList);
                break;
            case R.id.img_UserSongList:
                Log.d("June", "是否打开全部歌单-----" + mIsUserSongListOpen);
                ObjectAnimator objectAnimator;
                if (mIsUserSongListOpen) {
                    objectAnimator = ObjectAnimator.ofFloat(mUserSongListSwitch, "rotation", 180f, 360f);
                } else {
                    objectAnimator = ObjectAnimator.ofFloat(mUserSongListSwitch, "rotation", 0f, 180f);
                }
                objectAnimator.setDuration(300).start();

                mIsUserSongListOpen = !mIsUserSongListOpen;
                //recyclerview动画

                break;
            case R.id.bt_NullSongListSignal:
                Log.d("June", "创建歌单");
                break;
            default:
                break;

        }
    }

    private ServiceConnection connectionOverlayWindowServiceConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
            mOverlayWindowService = ((OverlayWindowService.OverlayWindowServiceBinder) iBinder).getService();
            mOverlayWindowService.changeStatus(OverlayWindowView.OverlayWindowBallStatus.INACTIVE);
            Log.d("JuneProcedure", getTag() + "----onServiceConnect----OverlayWindowService");
        }

        @Override
        public void onServiceDisconnected(ComponentName componentName) {
            Log.d("JuneProcedure", getTag() + "----onServiceDisconnected----OverlayWindowService");

        }
    };

    private class UserSongListAdapter extends RecyclerView.Adapter<UserSongListAdapter.UserSongListViewHolder> {
        private final ArrayList<SongList> mSongLists;

        public UserSongListAdapter(ArrayList<SongList> songLists) {
            mSongLists = songLists;
        }

        @NonNull
        @Override
        public UserSongListViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(mContext).inflate(R.layout.item_song_list, parent, false);
            return new UserSongListViewHolder(view);
        }

        @SuppressLint("SetTextI18n")
        @Override
        public void onBindViewHolder(@NonNull UserSongListViewHolder holder, int position) {
            holder.mSongList.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Log.d("June", "跳转到歌单");
                }
            });
            holder.mCover.setImageURI(Uri.fromFile(new File(mSongLists.get(position).getCover())));
            holder.mTitle.setText(mSongLists.get(position).getTitle());
            holder.mNumber.setText(mSongLists.get(position).getNumber() + "首");
        }

        @Override
        public int getItemCount() {
            return mSongLists == null ? 0 : mSongLists.size();
        }

        public class UserSongListViewHolder extends RecyclerView.ViewHolder {
            private final RelativeLayout mSongList;
            private final ImageView mCover;
            private final TextView mTitle;
            private final TextView mNumber;

            public UserSongListViewHolder(View view) {
                super(view);
                mSongList = view.findViewById(R.id.rl_SongList);
                mCover = view.findViewById(R.id.img_SongListCover);
                mTitle = view.findViewById(R.id.tv_SongListTitle);
                mNumber = view.findViewById(R.id.tv_SongListNumber);
            }
        }

    }
}